cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(MyLib VERSION 1.0.0 LANGUAGES CXX)

# 设置安装目录 (可选，默认为 /usr/local)
set(CMAKE_INSTALL_PREFIX /home/fly/workspace/mylib) 

# 设置库的类型 (STATIC, SHARED, MODULE)
add_library(MyLib SHARED mylib/mylib.cpp)

# 设置库的安装路径
set_target_properties(MyLib PROPERTIES
    PUBLIC_HEADER mylib/mylib.h
    DESTINATION include/mylib
    VERSION ${PROJECT_VERSION}
    SOVERSION 1 # 接口版本号, 修改接口时需要增加
)

# 安装头文件
install(
    FILES mylib/mylib.h
    DESTINATION include/mylib
)

# 安装库文件
install(TARGETS MyLib
    EXPORT MyLibTargets
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    PUBLIC_HEADER DESTINATION include
)

install(EXPORT MyLibTargets
    FILE MyLibConfig.cmake
    NAMESPACE MyLib::
    DESTINATION lib/cmake/MyLib
)

# 生成 MyLibConfigVersion.cmake 文件
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    ${CMAKE_CURRENT_BINARY_DIR}/MyLibConfigVersion.cmake
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMinorVersion
)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/MyLibConfigVersion.cmake
    DESTINATION lib/cmake/MyLib
)